

from lib.query import Query

class TopicsModel(Query):
    def __init__(self, db):
        self.db = db
        self.table_name = "topics"
        super(TopicsModel,self).__init__()

    def add_new_topics(self, info):
        return self.data(info).add()

    def get_topics_all(self, num=36, current_page=1):
        return self.pages(list_rows=num, current_page=current_page)


    def get_detail(self, topic_id):
        where = "topics.id = %s" % topic_id
        join = "LEFT JOIN user AS author_user ON topics.author_id = author_user.uid \
                LEFT JOIN node ON topics.node_id = node.id \
                LEFT JOIN user AS last_replied_user ON topics.last_replied_by = last_replied_user.uid"
        field = "topics.*, \
                author_user.username as author_username, \
                author_user.nickname as author_nickname, \
                author_user.avatar as author_avatar, \
                author_user.uid as author_uid, \
                author_user.reputation as author_reputation, \
                node.name as node_name, \
                node.slug as node_slug, \
                last_replied_user.username as last_replied_username, \
                last_replied_user.nickname as last_replied_nickname"
        return self.where(where).join(join).field(field).find()